﻿// <%= Ajax.Calendar("actionDate")%>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using FillCartridgeBBB.Models;

namespace FillCartridgeBBB.Controllers
{
    public class MoveController : Controller
    {
        private DataManager _dataManager;

        public MoveController (DataManager datamanager)
        {
            _dataManager = datamanager;
        }

        public ActionResult List()
        {
            ViewData.Model = _dataManager.Movement.SelectAll();
            return View();
        }

        public ActionResult Create ()
        {
            var departments = _dataManager.Departmets.SelectAll();
            var printers = _dataManager.Printers.SelectAll();
            var newCartridge = _dataManager.Cartridges.SelectByState(_dataManager.States.SelectById(1));
            var oldCartridge = _dataManager.Cartridges.SelectByState(_dataManager.States.SelectById(2));
            string cartridgeName = string.Empty;
            int res;
            int depId = int.TryParse(Request["Department"], out res) ? res : 0;
            int prtId = int.TryParse(Request["Printer"], out res) ? res : 0;
            int oldCartId = int.TryParse(Request["OldCartridge"], out res) ? res : 0;
            int newCartId = int.TryParse(Request["NewCartridge"], out res) ? res : 0;
            

            if (depId != 0)
            {
                IEnumerable<Printer> prts = _dataManager.Printers.SelectByDepartment(_dataManager.Departmets.SelectById(depId));
                printers = printers.Where(p => prts.Any(p2 => p2.PrinterId == p.PrinterId));
                departments = new List<Department>() { _dataManager.Departmets.SelectById(depId) };
            }

            if (prtId !=0)
            {
               // Printer p = _dataManager.Printers.SelectById(prtId);
                cartridgeName = _dataManager.Printers.SelectById(prtId).CartridgeName;
                printers = new List<Printer>() { _dataManager.Printers.SelectById(prtId) };

                departments =
                    departments.Where(
                        d =>
                        _dataManager.Departmets.SelectByPrinter(_dataManager.Printers.SelectById(prtId)).Any(
                            d2 => d2.DepartmentId == d.DepartmentId));
            }
            if (prtId != 0 || depId != 0)
            {
                newCartridge = newCartridge.Where(
                        c => _dataManager.Cartridges.SelectByPrinter(printers).Any(c2 => c2.CartridgeId == c.CartridgeId));
                oldCartridge =
                    oldCartridge.Where(
                        c =>
                        _dataManager.Cartridges.SelectByPrinter(printers).Any(c2 => c2.CartridgeId == c.CartridgeId));
            }
            ViewData["DepId"] = depId;
            ViewData["PrtId"] = prtId;
            ViewData["DepId"] = depId;
            ViewData["OldCartId"] = oldCartId;
            ViewData["NewCartId"] = newCartId;

            ViewData["Departments"] = departments;
            ViewData["Printers"] = printers;
            ViewData["CartridgeName"] = cartridgeName;
            ViewData["OldCartridges"] = oldCartridge;
            ViewData["NewCartridges"] = newCartridge;

            if (Request.IsAjaxRequest())
            {
                if (Request["Operation"] == "create")
                {
                    if (depId == 0)
                        ModelState.AddModelError("Department", "Виберіть підрозділ");
                    if (prtId == 0)
                        ModelState.AddModelError("Printer", "Виберіть принтер");
                    if (oldCartId == 0)
                        ModelState.AddModelError("OldCartridge", "Виберіть картридж для заміни");
                    if (newCartId == 0)
                        ModelState.AddModelError("NewCartridge", "Виберіть картридж на заміну");
                    if (ModelState.IsValid)
                    {
                        Move mov = new Move();
                        mov.Department = _dataManager.Departmets.SelectById(depId);
                        mov.Printer = _dataManager.Printers.SelectById(prtId);
                        mov.CartridgeOld = _dataManager.Cartridges.SelectById(oldCartId);
                        mov.CartridgeNew = _dataManager.Cartridges.SelectById(newCartId);
                        mov.DateMove = DateTime.Now;
                        _dataManager.Movement.Insert(mov);
                        return Content("Виконано!");
                    }
                }
                return PartialView("CreateMov", ViewData.Model);
            }
            return View();
        }
    }
}
